﻿namespace HostsIt
{
	using System;
	using System.Diagnostics.CodeAnalysis;
	using System.IO;
	using System.Windows.Forms;

	using HostsIt.Properties;

	public partial class MainForm : Form
	{
		private bool _reallyClose;

		public MainForm()
		{
			InitializeComponent();
		}

		protected override void OnLoad(EventArgs e)
		{
			if (!DesignMode)
			{
				SetResources();
			}
			base.OnLoad(e);
			LoadState();
			ValidateState();
		}

		[SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions", Justification = "I favor passing an owner over the desktop rules")]
		protected override void OnShown(EventArgs e)
		{
			base.OnShown(e);
			HideEditor();
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (!_reallyClose)
			{
				e.Cancel = true;
				Hide();
			}
		}

		private void SetResources()
		{
			this.Icon = Resources.App;
			this.Text = Resources.ApplicationName;

			notifier.Icon = Resources.App;
			notifier.Text = Resources.NotificationToolTip;
		}

		private void LoadState()
		{
			LoadBackupPath();
			LoadOutputPath();
		}

		private void LoadOutputPath()
		{
			txtOutput.Text = Presenter.PathToJSON;
		}

		private void LoadBackupPath()
		{
			txtBackupPath.Text = Presenter.BackupHostsFile;
		}

		private void ValidateState()
		{
			if (string.IsNullOrEmpty(txtBackupPath.Text))
			{
				Presenter.BackupHostsFile = Path.Combine(Presenter.DefaultUserStatePath, Resources.DefaultBackupFileName);
				LoadBackupPath();
			}

			if (!File.Exists(Presenter.BackupHostsFile))
			{
				try
				{
					Presenter.CreateStateDirectoryIfNecessary();
					File.Copy(Presenter.SystemHostsFileName, Presenter.BackupHostsFile, false);
				}
				catch (IOException)
				{
				}
			}

			if (string.IsNullOrEmpty(txtOutput.Text))
			{
				Presenter.PathToJSON = Path.Combine(Presenter.DefaultUserStatePath, Resources.DefaultJsonFileName);
				LoadOutputPath();
			}
		}

		private void miDisable_Click(object sender, EventArgs e)
		{
			if (miDisable.Checked)
			{
				Disable();
			}
			else
			{
				Enable();
			}
		}

		private void miExit_Click(object sender, EventArgs e)
		{
			_reallyClose = true;
			Disable();
			Application.Exit();
		}

		private void miEdit_Click(object sender, EventArgs e)
		{
			Edit();
		}

		private void Edit()
		{
			ShowEditor();
		}

		private void HideEditor()
		{
			ShowInTaskbar = false;
			Hide();
		}

		private void ShowEditor()
		{
			ShowInTaskbar = true;
			Show();
		}

		private void Enable()
		{
			notifier.Icon = Resources.App;
			Presenter.Enable();
		}

		private void Disable()
		{
			notifier.Icon = Resources.AppDisabled;
			Presenter.Disable();
		}

		private void btnBackupBrowse_Click(object sender, EventArgs e)
		{
			string fileName = Presenter.OpenFile("All Files|*.*", "Browse for the backup hosts file path",
												 Presenter.DefaultUserStatePath, Resources.DefaultBackupFileName);

			if (!String.IsNullOrEmpty(fileName))
			{
				txtBackupPath.Text = fileName;
			}
		}

		private void btnOutputBrowse_Click(object sender, EventArgs e)
		{
			string fileName = Presenter.SaveFile("Text Files|*.txt", "Browse for the JSON output file path",
												 Presenter.DefaultUserStatePath, Resources.DefaultJsonFileName);

			if (!String.IsNullOrEmpty(fileName))
			{
				txtOutput.Text = fileName;
			}
		}

		private void BtnSaveAndHideClick(object sender, EventArgs e)
		{
			Settings.Default.BackupHostsFile = new Uri(txtBackupPath.Text, UriKind.Absolute);
			Settings.Default.PathToJSON = new Uri(txtOutput.Text, UriKind.Absolute);

			Settings.Default.Save();
			HideEditor();
		}

		private void BtnCancelClick(object sender, EventArgs e)
		{
			LoadState();
			HideEditor();
		}
	}
}